<?php

/**
* @version $Id: lisitra.php 22/04/2006 07:54 Heriniaina Eugene $
* @package com_lisitra
* @copyright Copyright (C) 2006 serasera.org. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
*/

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

require_once( $mainframe->getPath( 'front_html', 'com_lisitra' ) );
require_once( $mainframe->getPath( 'class' ) );


$id			= intval( mosGetParam( $_REQUEST, 'id', 0 ) );
$sectionid 	= intval( mosGetParam( $_REQUEST, 'sectionid', 0 ) );
$pop 		= intval( mosGetParam( $_REQUEST, 'pop', 0 ) );
$limit 		= intval( mosGetParam( $_REQUEST, 'limit', '' ) );
$order 		= mosGetParam( $_REQUEST, 'order', '' );
$limitstart = intval( mosGetParam( $_REQUEST, 'limitstart', 0 ) );


$now = date( 'Y-m-d H:i', time() + $mosConfig_offset * 60 * 60 );

// cache activation

switch ( strtolower( $task ) ) {
	case 'view':
		showItem( $id, $option );
		break;

	case 'unsubscribe':
	case 'subscribe':
		$anarana = mosGetParam( $_REQUEST, 'anarana', '' );
		$adiresy = mosGetParam( $_REQUEST, 'adiresy', '' );
		sendmail( $id, $anarana, $adiresy, $option, $task );
		break;

	default:
		showlist();
		break;
}

function sendmail( $id, $anarana, $adiresy, $option, $task ) {
	global $database;

	if ( !$adiresy || ( is_email( $adiresy ) == false ) ) {
		mosErrorAlert( _LISITRA_FORM_NC );
		return;
	}
	$query = "SELECT *"
	. "\n FROM #__lisitra "
	. "\n WHERE id = $id AND published = 1"
	;
	$database->setQuery( $query );	
	$rows = $database->loadObjectList();

	$row = $rows[0];
	$emailservice = $row->$task;
	mosMail( $adiresy, $anarana , $emailservice, $mosConfig_fromname .': subscribe', 'void');

	$link = sefRelToAbs( 'index.php?option=com_lisitra&id='. $row->id );
	$link = ampReplace( $link );

	mosRedirect( $link, _LISITRA_THANK_MESSAGE );

}
/**
* Compiles a list of records
* @param database A database connector object
*/
function showlist( ) {
	global $database, $mainframe, $mosConfig_list_limit, $mosConfig_absolute_path, $Itemid;

	$search 	= $mainframe->getUserStateFromRequest( "search{$option}", 'search', '' );
	$search 	= $database->getEscaped( trim( strtolower( $search ) ) );

	$where = array();

	$where[] = "published = 1";
	if ($search) {
		$where[] = "LOWER(a.title) LIKE '%$search%'";
	}

	// get the total number of records
	$query = "SELECT COUNT(*)"
	. "\n FROM #_lisitra AS a"
	. (count( $where ) ? "\n WHERE " . implode( ' AND ', $where ) : "")
	;

	$database->setQuery( $query );
	$total = $database->loadResult();
	$limit = $limit ? $limit : $mosConfig_list_limit;
	if ( $total <= $limit ) $limitstart = 0;

	require_once( $mosConfig_absolute_path . '/includes/pageNavigation.php' );
	$pageNav = new mosPageNav( $total, $limitstart, $limit );

	$query = "SELECT a.*"
	. "\n FROM #__lisitra AS a"
	. ( count( $where ) ? "\n WHERE " . implode( ' AND ', $where ) : "")
	. "\n ORDER BY a.ordering"
	. "\n LIMIT $pageNav->limitstart, $pageNav->limit"
	;
	$database->setQuery( $query );

	$rows = $database->loadObjectList();
	if ($database->getErrorNum()) {
		echo $database->stderr();
		return false;
	}


	// Parameters
	$menu = new mosMenu( $database );
	$menu->load( $Itemid );
	$menu_params = new mosParameters( $menu->params );

	$menu_params->def( 'page_title', 1 );
	$menu_params->def( 'header', $menu->name );
	$menu_params->def( 'pageclass_sfx', '' );


	// build list

	HTML_Lisitra::showlisitra( $params, $rows, $tabclass, $menu_params );
}

function showItem ( $id, $option) {
	global $database;

	//Record the hit
	$query = "SELECT *"
	. "\n FROM #__lisitra "
	. "\n WHERE id = $id AND published = 1"
	;
	$database->setQuery( $query );	
	$rows = $database->loadObjectList();

	// Parameters
	$menu = new mosMenu( $database );
	$menu->load( $Itemid );
	$params = new mosParameters( $menu->params );
	//$params->def( 'page_title', 1 );

	
	if ( $rows ) {
		// redirects to url if matching id found
		HTML_Lisitra::showItem( $option, $rows[0], $params);
	} else {		
		// redirects to weblink category page if no matching id found
		showlist();
	}
}

function is_email($email){
	$rBool=false;

	if  ( preg_match( "/[\w\.\-]+@\w+[\w\.\-]*?\.\w{1,4}/" , $email ) ){
		$rBool=true;
	}
	return $rBool;
}

?>
